package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.domain.TaskType;
import com.ruoyi.system.mapper.TaskTypeMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TaskThreeMapper;
import com.ruoyi.system.domain.TaskThree;
import com.ruoyi.system.service.ITaskThreeService;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-18
 */
@Service
public class TaskThreeServiceImpl implements ITaskThreeService 
{
    @Autowired
    private TaskThreeMapper taskThreeMapper;

    @Autowired
    private TaskTypeMapper taskTypeMapper;

    private static final Logger log = LoggerFactory.getLogger(TaskThreeServiceImpl.class);

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public TaskThree selectTaskThreeById(Long id)
    {
        return taskThreeMapper.selectTaskThreeById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param taskThree 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TaskThree> selectTaskThreeList(TaskThree taskThree)
    {
        return taskThreeMapper.selectTaskThreeList(taskThree);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param taskThree 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertTaskThree(TaskThree taskThree)
    {


        //案件类型
        QueryWrapper<TaskType> queryWrapper = new QueryWrapper();
        queryWrapper.select("type_name")
                .eq("type_name",taskThree.getType())
                .eq("task_index",3);
        List<TaskType> typeList = taskTypeMapper.selectList(queryWrapper);
        //空则新增
        if (CollectionUtils.isEmpty(typeList)){
            TaskType taskType = new TaskType();
            taskType.setTypeName(taskThree.getType());
            taskType.setTaskIndex(3L);
            taskTypeMapper.insertTaskType(taskType);
        }



        taskThree.setCreateTime(DateUtils.getNowDate());
        return taskThreeMapper.insertTaskThree(taskThree);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param taskThree 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTaskThree(TaskThree taskThree)
    {

        //案件类型
        QueryWrapper<TaskType> queryWrapper = new QueryWrapper();
        queryWrapper.select("type_name")
                .eq("type_name",taskThree.getType())
                .eq("task_index",3);
        List<TaskType> typeList = taskTypeMapper.selectList(queryWrapper);
        //空则新增
        if (CollectionUtils.isEmpty(typeList)){
            TaskType taskType = new TaskType();
            taskType.setTypeName(taskThree.getType());
            taskType.setTaskIndex(3L);
            taskTypeMapper.insertTaskType(taskType);
        }

        taskThree.setUpdateTime(DateUtils.getNowDate());
        return taskThreeMapper.updateTaskThree(taskThree);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTaskThreeByIds(Long[] ids)
    {
        return taskThreeMapper.deleteTaskThreeByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTaskThreeById(Long id)
    {
        return taskThreeMapper.deleteTaskThreeById(id);
    }





    /**
     * 导入数据
     *
     * @param taskThreeList 数据列表
     * @return 结果
     */
    @Override
    public String importUser(List<TaskThree> taskThreeList)
    {
        if (StringUtils.isNull(taskThreeList) || taskThreeList.size() == 0)
        {
            throw new ServiceException("导入账号密码数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TaskThree taskThree : taskThreeList)
        {
            try
            {
                LoginUser loginUser = SecurityUtils.getLoginUser();

                if (taskThree.getTaskId() == null){
                    failureNum++;
                    failureMsg.append( "任务中心编号不能为空 "+"\n");
                }

                if (StringUtils.isEmpty(taskThree.getType())){
                    failureNum++;
                    failureMsg.append( "类型不能为空 "+"\n");
                }

                if (StringUtils.isEmpty(taskThree.getHostName())){
                    failureNum++;
                    failureMsg.append( "主机名不能为空 "+"\n");
                }

                if (StringUtils.isEmpty(taskThree.getAddress())){
                    failureNum++;
                    failureMsg.append( "地址不能为空 "+"\n");
                }

                taskThree.setCreateName(loginUser.getUser().getNickName());
                taskThree.setCreateTime(new Date());
            }
            catch (Exception e)
            {
                failureNum++;

                failureMsg.append(e.getMessage());
                log.error(String.valueOf(e));
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");


            taskThreeMapper.batchInsertThree(taskThreeList);

        }
        return successMsg.toString();
    }









}
